package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.PingLunActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.base.BaseRVAdapter;
import com.wnhz.work.yz.base.BaseViewHolder;
import com.wnhz.work.yz.bean.YanShouShouYeBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.pull.PullLoadMoreRecyclerView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class YanShouAllActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView title;
    private View head;
    private PullLoadMoreRecyclerView home;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_shou_all);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("验收");
        head = View.inflate(this, R.layout.yanshou_head, null);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
//        ImageView search_imgs = (ImageView) findViewById(R.id.search_imgs);
//        search_imgs.setImageResource(R.mipmap.ic_erweimasaomiao);
//        findViewById(R.id.ll_search).setVisibility(View.VISIBLE);
//        findViewById(R.id.ll_search).setOnClickListener(this);
        initView();
    }

    private List<TestBean> list = new ArrayList<>();
    private String[] str = {
            "验收", "全装标准",
            "整改", "厨房标准",
            "上传", "浴室标准",
            "任务榜", "验收报告",
            "项目处罚", "稽查报告"};

    private int[] imt = {R.mipmap.ic_home_check2x, R.mipmap.ic_home_all2x,
            R.mipmap.ic_home_rectify2x, R.mipmap.ic_home_kitchen2x,
            R.mipmap.ic_home_uploading2x, R.mipmap.ic_home_bathroom2x,
            R.mipmap.ic_home_task2x, R.mipmap.ic_home_report2x,
            R.mipmap.ic_home_punish2x, R.mipmap.ic_home_jicha2x
    };

    private void initView() {
        for (int i = 0; i < str.length; i++) {
            TestBean b = new TestBean();
            b.setNums(str[i]);
            b.setInt_img(imt[i]);
            list.add(b);
        }
        title = (RecyclerView) head.findViewById(R.id.title);
        title.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.HORIZONTAL, false));
        title.setAdapter(new BaseRVAdapter(this, list) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_yanshou;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                holder.setTextView(R.id.text, list.get(position).getNums());
                holder.getImageView(R.id.img).setImageResource(list.get(position).getInt_img());

                holder.getView(R.id.ll_max).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (position) {
                            //待验收
                            case 0:
                                startActivity(new Intent(YanShouAllActivity.this, YanShouDaiActivity.class));
                                break;
                            //待整改
                            case 2:
                                startActivity(new Intent(YanShouAllActivity.this, YanShouGaiActivity.class));
                                break;
                            //待上传
                            case 4:
                                // startActivity(new Intent(YanShouAllActivity.this, YanShouChuanActivity.class));
                                MyToast("此功能正在开发");
                                break;
                            //任务榜
                            case 6:
                                startActivity(new Intent(YanShouAllActivity.this, YanShouRenWuActivity.class));
                                break;
//                            //已报检
//                            case 8:
//                                startActivity(new Intent(YanShouAllActivity.this, YanShouBaoJianActivity.class));
//                                break;

                            //项目处罚
                            case 8:
                                startActivity(new Intent(YanShouAllActivity.this, YanShouChuFaActivity.class));
                                break;
                            //全装标准
                            case 1:
                                startActivity(new Intent(YanShouAllActivity.this, YanShouQuanBiaoZhunActivity.class));
                                break;
                            //厨房标准
                            case 3:
                                startActivity(new Intent(YanShouAllActivity.this, YanShouChuBiaoZhunActivity.class));
                                break;
                            //浴室标准
                            case 5:
                                startActivity(new Intent(YanShouAllActivity.this, YanShouBiaoZhunActivity.class));
                                break;

                            //验收报告
                            case 7:
                                MyToast("此功能正在开发");
                                //startActivity(new Intent(YanShouAllActivity.this, YanShouBaoGaoActivity.class));
                                break;
//                            //"整改分配",
//                            case 9:
//                                startActivity(new Intent(YanShouAllActivity.this, YanShouXuZhengGaiActivity.class));
//                                break;
                            //稽查
                            case 9:
                                startActivity(new Intent(YanShouAllActivity.this, YanShouJiChaActivity.class));
                                break;
                        }
                    }
                });
            }
        });
//        upLoad();
        home = (PullLoadMoreRecyclerView) findViewById(R.id.home);
        home.setLinearLayout();
        homeAdapter = new BaseRVAdapter(this, info1) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_yanshou_home;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                x.image().bind(holder.getImageView(R.id.tou), info1.get(position).getHeadimg());
                holder.setTextView(R.id.name, info1.get(position).getRealname());
                holder.setTextView(R.id.info, info1.get(position).getInfo());
                holder.setTextView(R.id.yanshoumingzi, info1.get(position).getLevel());
                holder.setTextView(R.id.company, info1.get(position).getCompany());
                holder.setTextView(R.id.hao, info1.get(position).getBudget_name());
                holder.setTextView(R.id.address, info1.get(position).getAddress());
                holder.setTextView(R.id.sj, info1.get(position).getStart_time(
                ));
                RecyclerView recycler = holder.getView(R.id.recycler);
                final ImageView img = holder.getImageView(R.id.img);

                if (info1.get(position).getImg() != null && info1.get(position).getImg().size() > 1) {
                    recycler.setVisibility(View.VISIBLE);
                    img.setVisibility(View.GONE);
                    recycler.setLayoutManager(new GridLayoutManager(YanShouAllActivity.this, 3, LinearLayoutManager.VERTICAL, false));
                    recycler.setAdapter(new BaseRVAdapter(YanShouAllActivity.this, info1.get(position).getImg()) {
                        @Override
                        public int getLayoutId(int viewType) {
                            return R.layout.item_img_recycler3;
                        }

                        @Override
                        public void onBind(BaseViewHolder holder, final int position2) {
                            ImageLoader.getInstance().displayImage(info1.get(position).getImg().get(position2), holder.getImageView(R.id.img));


                            //  x.image().bind(holder.getImageView(R.id.img), info1.get(position).getImg().get(position2));
                            holder.getView(R.id.img).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Intent intent = new Intent(YanShouAllActivity.this, ShowImageDetail.class);
                                    intent.putStringArrayListExtra("paths", (ArrayList<String>) info1.get(position).getImg());
                                    intent.putExtra("index", position2);
                                    startActivity(intent);
                                }
                            });
                        }
                    });
                } else if (info1.get(position).getImg() != null && info1.get(position).getImg().size() == 1) {
                    img.setVisibility(View.VISIBLE);
                    recycler.setVisibility(View.GONE);
                    //    x.image().bind(holder.getImageView(R.id.img), info1.get(position).getImg().get(0));
                    ImageLoader.getInstance().displayImage(info1.get(position).getImg().get(0), holder.getImageView(R.id.img));

                    // ImageLoadUrl.downloadImgByUrl(info1.get(position).getImg().get(0), holder.getImageView(R.id.img));

                } else {
                    img.setVisibility(View.GONE);
                    recycler.setVisibility(View.GONE);
                }
                img.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(YanShouAllActivity.this, ShowImageDetail.class);
                        intent.putStringArrayListExtra("paths", (ArrayList<String>) info1.get(position).getImg());
                        intent.putExtra("index", 0);
                        startActivity(intent);
                    }
                });

                RecyclerView pl = holder.getView(R.id.pl);
                pl.setLayoutManager(new LinearLayoutManager(YanShouAllActivity.this, LinearLayoutManager.VERTICAL, false));
                final List<YanShouShouYeBean.InfoEntity.CommentEntity> comment = info1.get(position).getComment();
                pl.setAdapter(new BaseRVAdapter(YanShouAllActivity.this, comment) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.item_piinlgun;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, int position) {
                        holder.setTextView(R.id.name, comment.get(position).getRealname()+"：");
                        holder.setTextView(R.id.content, comment.get(position).getContent());
                      //  holder.setTextView(R.id.sj, comment.get(position).getAdd_time());
                       // x.image().bind(holder.getImageView(R.id.tou), comment.get(position).getHeadimg());

//
//                        final List<String> img = comment.get(position).getImg();
//
//                        LinearLayout status1 = holder.getView(R.id.status1);
//                        LinearLayout status2 = holder.getView(R.id.status2);
//                        LinearLayout status3 = holder.getView(R.id.status3);
//                        ImageView img1 = holder.getImageView(R.id.img1);
//                        ImageView img2_1 = holder.getImageView(R.id.img2_1);
//                        ImageView img2_2 = holder.getImageView(R.id.img2_2);
//                        RecyclerView img_recycler = holder.getView(R.id.img_recycler);
//                        switch (img.size()) {
//                            case 0:
//                                status1.setVisibility(View.GONE);
//                                status2.setVisibility(View.GONE);
//                                status3.setVisibility(View.GONE);
//
//
//                                break;
//                            case 1:
//                                status1.setVisibility(View.VISIBLE);
//                                status2.setVisibility(View.GONE);
//                                status3.setVisibility(View.GONE);
//                                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
//                                        img1);
//                                img1.setOnClickListener(new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {
//                                        Intent intent = new Intent(YanShouAllActivity.this, ShowImageDetail.class);
//                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
//                                        intent.putExtra("index", 0);
//                                        startActivity(intent);
//                                    }
//                                });
//                                break;
//                            case 2:
//                                status1.setVisibility(View.GONE);
//                                status2.setVisibility(View.VISIBLE);
//                                status3.setVisibility(View.GONE);
//                                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(0),
//                                        img2_1);
//                                com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(img.get(1),
//                                        img2_2);
//                                img2_1.setOnClickListener(new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {
//                                        Intent intent = new Intent(YanShouAllActivity.this, ShowImageDetail.class);
//                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
//                                        intent.putExtra("index", 0);
//                                        startActivity(intent);
//                                    }
//                                });
//                                img2_2.setOnClickListener(new View.OnClickListener() {
//                                    @Override
//                                    public void onClick(View v) {
//                                        Intent intent = new Intent(YanShouAllActivity.this, ShowImageDetail.class);
//                                        intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
//                                        intent.putExtra("index", 1);
//                                        startActivity(intent);
//                                    }
//                                });
//                                break;
//                            default:
//                                status1.setVisibility(View.GONE);
//                                status2.setVisibility(View.GONE);
//                                status3.setVisibility(View.VISIBLE);
//                                img_recycler.setLayoutManager(new GridLayoutManager(YanShouAllActivity.this, 3, LinearLayoutManager.VERTICAL, false));
//                                img_recycler.setAdapter(new BaseRVAdapter(YanShouAllActivity.this, img) {
//                                    @Override
//                                    public int getLayoutId(int viewType) {
//                                        return R.layout.item_photo;
//                                    }
//
//                                    @Override
//                                    public void onBind(BaseViewHolder holder, final int position) {
//                                        holder.getImageView(R.id.img).setOnClickListener(new View.OnClickListener() {
//                                            @Override
//                                            public void onClick(View v) {
//                                                Intent intent = new Intent(YanShouAllActivity.this, ShowImageDetail.class);
//                                                intent.putStringArrayListExtra("paths", (ArrayList<String>) img);
//                                                intent.putExtra("index", position);
//                                                startActivity(intent);
//                                            }
//                                        });
//                                        ImageLoader.getInstance().displayImage(img.get(position), holder.getImageView(R.id.img));
//
//                                    }
//
//                                });
//
//                                break;
//                        }
                    }
                });

                holder.getView(R.id.pinglun).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(YanShouAllActivity.this, PingLunActivity.class).putExtra("pid", info1.get(position).getPid()).putExtra("pinglun", 1));
                    }
                });
            }
        };
        homeAdapter.addHeaderView(head);
        home.setAdapter(homeAdapter);
        upLoad(0);
        home.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {

            @Override
            public void onRefresh() {
                upLoad(0);
            }

            @Override
            public void onLoadMore() {
                upLoad(1);
            }
        });
    }


    private List<YanShouShouYeBean.InfoEntity> info1 = new ArrayList<>();

    @Override
    protected void onResume() {
        upLoad(0);
        super.onResume();
    }

    private int pager = 0;

    private void upLoad(final int i) {
        Map<String, Object> map = new HashMap<>();
        if (i == 0) {
            pager = 0;
        } else {
            pager++;
        }

        map.put("page", pager);

        XUtil.Post(Url.DECORATE_CHECKINDEX, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    Log.e("----", "onSuccess:= " + info);
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        YanShouShouYeBean bean = gson.fromJson(result, YanShouShouYeBean.class);
                        List<YanShouShouYeBean.InfoEntity> info2 = bean.getInfo();
                        if (i == 0) {
                            info1.clear();
                        }
                        info1.addAll(info2);
                        homeAdapter.notifyDataSetChanged();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                home.setPullLoadMoreCompleted();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
                ex.printStackTrace();
            }
        });


    }

    private BaseRVAdapter homeAdapter;


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
//            case R.id.ll_search:
//                startActivityForResult(new Intent(this, CaptureActivity.class), 0);
//                break;
        }
    }

//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == 0)
//            if (resultCode == RESULT_OK) {
//                //处理扫描结果（在界面上显示）
//                if (null != data) {
//                    Bundle bundle = data.getExtras();
//                    if (bundle == null) {
//                        return;
//                    }
//                    if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) {
//                        String erwei = bundle.getString(CodeUtils.RESULT_STRING);
//                        Toast.makeText(this, "" + erwei, Toast.LENGTH_LONG).show();
//                    } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) {
//                        Toast.makeText(this, "解析二维码失败", Toast.LENGTH_LONG).show();
//                    }
//                }
//            }
//    }
}
